<?php
namespace app\admin\controller;
use think\facade\Cache;
class Other extends Base{

    public function index($path=''){
        if($path!=''){
            return $this->fetch($path);
        }else{
            return $this->fetch();
        }
    }

    //$class 需要对父级窗口发生内容改变的class
    //$num 需要上传的数量 1为单张 2为多选
    //$index 父级窗口列表数据的index
    public function img_list($class,$num,$index=0){
        $path = './uploads';
        // $list = $this->scan_dir($path);
        // $this->assign('dirData',json_encode($list));
        $pt = input("post.");
        if(count($pt)){
            $files = array_diff(scandir($path.'/'.$pt['path']), array('.', '..'));
            return json($files);
        }


        if ($dh = opendir($path)) {
            $i = 0;
            while (($file = readdir($dh)) !== false) {
                if ($file != "." && $file != "..") {
                    $files[$i]["name"] = $file;//获取文件名称
                    $files[$i]["time"] = date("Y-m-d H:i:s",filemtime($path . "/" . $file));//获取文件最近修改日期
                    $i++;
                }
            }
        }
        closedir($dh);
        foreach($files as $k=>$v){
            $time[$k] = $v['time'];
            $name[$k] = $v['name'];
        }
        array_multisort($time,SORT_DESC,SORT_STRING, $files);//按时间排序
        

        $list = array_diff(scandir($path.'/'.$files[0]['name']), array('.', '..'));
        $this->assign('list',json_encode($list));
        $this->assign('dirData',json_encode($files));
        $this->assign('class',$class);
        $this->assign('num',$num);
        $this->assign('index',$index);

        return $this->fetch();
    }

    public function del_img(){
        $pt = input("post.");
        if (!unlink('.'.$pt['path'])){
            $this->info['msg'] = '删除失败';
            return json($this->info);
        }else{
            $this->logData('删除图片：'.$pt['path']);
            $this->info['code'] = 1;
            $this->info['msg'] = '删除成功';
            return json($this->info);
        }
    }


    public function upImg(){
        $file = request()->file('file');
        $savename = \think\facade\Filesystem::putFile( 'uploads', $file);
        $this->info['code'] = 1;
        $this->info['msg'] = '上传成功';
        $this->info['data'] = $savename;
        $this->logData('上传图片：'.$savename);
        return json($this->info);
    }

    //清除数据库
    public function clear_sql_data(){
        Cache::clear(); 
        $this->info = ['code'=>1,'msg'=>'操作成功'];
        return json($this->info);
    }

    
    //微信编辑器加载模板
	public function loadtemp(){
		$type = trim(input('post.type'));
		if(!in_array($type,array('title','text','img','tpl','scene','follow')))
		{
			echo '错误的模板类型 T_T';
			exit;
		}
        return $this->fetch('other/wx_temp/temp_'.$type);
	}

//     /**
//    * 递归获取文件夹内所有文件
//    * 返回一个TREE结构的文件系统
//    * @param string $dir
//    * @param array $filter
//    * @return array $files
//    */
//   public function scan_dir($dir, $filter = array()){
//         if(!is_dir($dir))return false;
//         $files = array_diff(scandir($dir), array('.', '..'));
//         if(is_array($files)){
//             foreach($files as $key=>$value){
//                 if(is_dir($dir . '/' . $value)){
//                     $files[$value] = $this->scan_dir($dir . '/' . $value, $filter);
//                     unset($files[$key]);
//                     continue;
//                 }

//                 $pathinfo = pathinfo($dir . '/' . $value);
//                 $extension = array_key_exists('extension', $pathinfo) ? $pathinfo['extension'] : '';
//                 if(!empty($filter) && !in_array($extension, $filter)){
//                     unset($files[$key]);
//                 }
//             }
//         }
//         unset($key, $value);
//         return $files;
//     }
}
